package com.approval.approvalapp.index.dbsx.qjsp.fragment;


import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.QjBean;
import com.approval.approvalapp.bean.QjDbBean;
import com.approval.approvalapp.index.dbsx.qjsp.QjSpActivity;
import com.approval.approvalapp.index.dbsx.qjsp.adapter.QjDbAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseFragment;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 * 请假审批代办
 * A simple {@link Fragment} subclass.
 */
public class QjspDbFragment extends BaseFragment {


    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    private int pagenum = 1;
    private QjDbAdapter qjDbAdapter;
    private List<QjBean.StayBean> stay = new ArrayList<>();
    public static QjspDbFragment instance;
    @Bind(R.id.empty_ll)
    LinearLayout em;

    @Override
    public int getLayoutId() {

        instance = this;
        return R.layout.fragment_qjsp_db;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        instance = null;
    }

    @Override
    public void initView(View view) {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        qjDbAdapter = new QjDbAdapter();
        xrv.setAdapter(qjDbAdapter);


        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                pagenum = pagenum + 1;
                xrv.setNoMore(true);
            }
        });
    }


    public void refreshData() {
        pagenum = 1;
        JSONObject jsonObject = new JSONObject();
        String s1 = "v1/center/leave_list_already_zx";
        String s2 = "v1/center/leave_list_already";
        String s3 = "v1/center/leave_list_already_zx_t";
        String url;
        if (SpUtil.getString(this.getActivity(), SpUtil.Usertype, "")
                .equals("1")) {
            Log.d("QjspDbFragmensst", QjSpActivity.instance.uriid);
            if (QjSpActivity.instance.uriid.equals("1")) {
                url = s3;
                jsonObject.put("org_code", SpUtil.getString(this.getActivity(), SpUtil.ORG_CODE, ""));
                jsonObject.put("GZFW", SpUtil.getString(this.getActivity(), SpUtil.GZFW1, ""));
            }else {
                url = s1;
                jsonObject.put("region_code", SpUtil.getString(this.getActivity(), SpUtil.REGION_CODE, ""));
            }
            jsonObject.put("if_use", "0");
        } else {
            url = s2;
            jsonObject.put("org_code", SpUtil.getString(this.getActivity(), SpUtil.ORG_CODE, ""));
            jsonObject.put("if_use", "0");
        }
        Log.d("QjspDbFragmentss1", jsonObject.toString());
        NetRequsetUtil.getInstance().netRequestPostJson(url, jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                Log.d("QjspDbFragmentss", str);
                stay = JSON.parseArray(str, QjBean.StayBean.class);

                if (stay == null || stay.size() == 0){
                    em.setVisibility(View.VISIBLE);
                }else {
                    em.setVisibility(View.GONE);
                }

                qjDbAdapter.setListBeans(stay);
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void initData() {

    }


}
